package game.module.qzmj.processor;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import db.proto.ProtoMessageQzmj.ARound;
import db.proto.ProtoMessageQzmj.AllAction;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;

import game.module.qzmj.bean.QzmjRound;
import game.module.qzmj.logic.QzmjDatamanager;
import game.module.qzmj.logic.QzmjLogic;
import game.module.qzmj.logic.QzmjRoomManager;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageQzmj.C2SVoteDissQzmjroom;
import lua.LuaMessageQzmj.PushVoteDissQzmjroom;
import lua.LuaMessageQzmj.S2CVoteDissQzmjroom;

@MsgCodeAnn(msgcode = C2SVoteDissQzmjroom.id, accessLimit = 500)
public class QzmjForceDismissRoom extends PlayingRoleMsgProcessor{

	private static Logger logger = LoggerFactory.getLogger(QzmjForceDismissRoom.class);

	
	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		logger.info("qzmj force to dismiss room,playerId={}", playerId);
		C2SVoteDissQzmjroom reqMsg = C2SVoteDissQzmjroom.parse(requestMessage);
		byte vote = reqMsg.vote;
		// 房间是否存在
		QzmjRound mjround = QzmjDatamanager.getInstance().getMjRoom(playerId);
		if (mjround == null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		List<Integer> players = mjround.getPlayerIds();
		boolean votediss[] = mjround.getVotediss();
		int currents = players.indexOf(playerId);
		
		S2CVoteDissQzmjroom msg = new S2CVoteDissQzmjroom();
		playingRole.writeAndFlush(msg.build(playingRole.alloc()));
		
		PushVoteDissQzmjroom pushmsg = new PushVoteDissQzmjroom();
		pushmsg.index = (byte)currents;
		pushmsg.result = vote;
		ARound around = mjround.getARound();
		ARound.Builder modround = around.toBuilder();
		AllAction.Builder thisAction = AllAction.newBuilder();
		thisAction.setUserAction(10);
		thisAction.setPlayerIndex(currents);
		thisAction.setVote(vote);
		AllAction realaction =thisAction.build();
		modround.addActions(realaction);
		ARound realroud =  modround.build();
		mjround.setARound(realroud);
		if(vote == 0) {
			//不解散
			QzmjRoomManager.getInstance().cancledismiss(mjround.getRoomUuid());
			mjround.setCreateVotePlayerID((byte)0);
			mjround.setStartvoteTime(0);
			Arrays.fill(votediss,false);
		}else {
			QzmjRoomManager.getInstance().createtRemoveMjroom(playerId);//存在任务 不会新加
			votediss[currents] = true;
		}
		for(Integer aplayer:players) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aplayer);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushmsg.build(pr.alloc()));
				// 推送关于出牌的信息
			}
		}
		for(boolean flag:votediss) {
			if(!flag)
				return; //还有人未投票，结束
		}
		QzmjRoomManager.getInstance().advanceCancel(playerId);
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
